﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using RoadFlow.Utility;

namespace RoadFlow.Mvc.Areas.RoadFlowCore.Controllers
{
    [Area("RoadFlowCore")]
    public class FlowApiSystemController : Controller
    {
        [Validate]
        public IActionResult Index()
        {
            var flowApiSystems = new Business.FlowApiSystem().GetAll();
            Newtonsoft.Json.Linq.JArray jArray = new Newtonsoft.Json.Linq.JArray();
            foreach (var flowApiSystem in flowApiSystems)
            {
                Newtonsoft.Json.Linq.JObject jObject = new Newtonsoft.Json.Linq.JObject
                {
                    { "id", flowApiSystem.Id },
                    { "Name", flowApiSystem.Name },
                    { "SystemCode", flowApiSystem.SystemCode },
                    { "SystemIP", flowApiSystem.SystemIP },
                    { "Note", flowApiSystem.Note },
                    { "Sort", flowApiSystem.Sort },
                    { "Opation", "<a class=\"list\" href=\"javascript:void(0);\" onclick=\"edit('" + flowApiSystem.Id + "');return false;\"><i class=\"fa fa-edit (alias)\"></i>编辑</a>" }
                };
                jArray.Add(jObject);
            }

            ViewData["appId"] = Request.Querys("appid");
            ViewData["tabId"] = Request.Querys("tabid");
            ViewData["json"] = jArray.ToString();
            return View();
        }

        [Validate]
        public IActionResult Edit()
        {
            string systemId = Request.Querys("systemid");
            Business.FlowApiSystem flowApiSystem = new Business.FlowApiSystem();
            Model.FlowApiSystem flowApiSystemModel = null;
            if (systemId.IsGuid(out Guid bid))
            {
                flowApiSystemModel = flowApiSystem.Get(bid);
            }
            if (null == flowApiSystemModel)
            {
                flowApiSystemModel = new Model.FlowApiSystem
                {
                    Id = Guid.NewGuid(),
                    Sort = flowApiSystem.GetMaxSort()
                };
            }
            ViewData["queryString"] = Request.UrlQuery();
            return View(flowApiSystemModel);
        }

        [Validate]
        [ValidateAntiForgeryToken]
        public string Save(Model.FlowApiSystem flowApiSystemModel)
        {
            if (!ModelState.IsValid)
            {
                return Tools.GetValidateErrorMessag(ModelState);
            }
            Business.FlowApiSystem flowApiSystem = new Business.FlowApiSystem();
            if (Request.Querys("systemid").IsGuid(out Guid guid))
            {
                var oldModel = flowApiSystem.Get(guid);
                string oldJSON = null == oldModel ? "" : oldModel.ToString();
                flowApiSystem.Update(flowApiSystemModel);
                Business.Log.Add("修改了接口系统-" + flowApiSystemModel.Name, type: Business.Log.Type.流程管理, oldContents: oldJSON, newContents: flowApiSystemModel.ToString());
            }
            else
            {
                flowApiSystem.Add(flowApiSystemModel);
                Business.Log.Add("添加了接口系统-" + flowApiSystemModel.Name, flowApiSystemModel.ToString(), Business.Log.Type.流程管理);
            }
            return "保存成功!";
        }

        [Validate]
        [ValidateAntiForgeryToken]
        public string Delete()
        {
            string ids = Request.Forms("ids");
            List<Model.FlowApiSystem> flowApiSystems = new List<Model.FlowApiSystem>();
            Business.FlowApiSystem flowApiSystem = new Business.FlowApiSystem();
            var allFlowApiSystems = flowApiSystem.GetAll();
            foreach (string id in ids.Split(','))
            {
                if (!id.IsGuid(out Guid bid))
                {
                    continue;
                }
                var model = allFlowApiSystems.Find(p => p.Id == bid);
                if (null == model)
                {
                    continue;
                }
                flowApiSystems.Add(model);
            }
            flowApiSystem.Delete(flowApiSystems.ToArray());
            Business.Log.Add("删除了接口系统", Newtonsoft.Json.JsonConvert.SerializeObject(flowApiSystems), Business.Log.Type.流程管理);
            return "删除成功!";
        }

        public string CheckCode()
        {
            string id = Request.Querys("id");
            string value = Request.Forms("value");
            return new Business.FlowApiSystem().ValidateSystemCode(id.ToGuid(), value) ? "1" : "系统标识重复!";
        }
    }
}